package com.opd.soeghtool;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.PixelFormat;
import android.hardware.usb.UsbManager;
import android.os.Build;
import android.os.IBinder;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Locale;


public class UserGuideService extends Service {

    final String TAG ="SOEUGS";

    ImageView iv_user_guide_btn,iv_user_guide_ring;
    TextView tv_tip_user_guide,tv_tip_user_guide_detail;
    Button btn_skip_user_guide,btn_user_guide_next;
    int mImgBtnSize = 50,mImgRingSize = 75;
    int panelWidth,panelHeight;

    private MyDrawView gameView;
    WindowManager windowManager;
    WindowManager.LayoutParams paramsDraw,paramsGuide,paramsAdvance;
    AbsoluteLayout layout_advance_setting,guideLayout;
    LinearLayout layout_advance_setting_step3;
    RelativeLayout layout_user_guide_tip;
    GlobalVar mGlobalVar;
    MyBroadcastReceive myBroadcastReceive;
    boolean bIsBigIcon = false;
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void onCreate()
    {
        super.onCreate();

        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        display.getRealMetrics(dm);
        mGlobalVar = (GlobalVar)getApplication();
        paramsDraw = new WindowManager.LayoutParams();
        paramsGuide = new WindowManager.LayoutParams();
        paramsAdvance = new WindowManager.LayoutParams();
        windowManager = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE);
        paramsAdvance.format =paramsGuide.format = paramsDraw.format = PixelFormat.RGBA_8888;
        //设置flags.不可聚焦及不可使用按钮对悬浮窗进行操控.
        paramsDraw.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        paramsAdvance.flags = paramsGuide.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            paramsAdvance.type = paramsDraw.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
            paramsGuide.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            paramsAdvance.type =paramsDraw.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
            paramsGuide.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        }


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            paramsDraw.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
            paramsGuide.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
        }
        //设置窗口初始停靠位置.
        paramsAdvance.gravity = paramsDraw.gravity = Gravity.START | Gravity.TOP;
        paramsGuide.gravity = Gravity.START | Gravity.TOP;

        Log.d(TAG,String.format("wid=%d, hei=%d",dm.widthPixels,dm.heightPixels));
        panelHeight = dm.heightPixels;
        panelWidth = dm.widthPixels;
        if(Transfer_Con.ENABLE_DIFFERENT_RESOLUTION_GB) {
            mGlobalVar.tranSet_Value(Transfer_Con.HID_CMD_SET_SCREEN_RESOLUTION, panelWidth, panelHeight);
            /*
            if ((mGlobalVar.getScreenWidth() != panelHeight) && ((panelHeight == 1128) || (panelHeight == 752))) {
                panelHeight = mGlobalVar.getScreenWidth();
            }

            if ((mGlobalVar.getScreenHeight() != panelWidth) && ((panelWidth == 2400) || (panelWidth == 1600))) {
                panelWidth = mGlobalVar.getScreenHeight();
            }

             */
        }

        paramsAdvance.width = paramsGuide.width = panelWidth;
        paramsAdvance.height =paramsGuide.height = panelHeight;

        Log.d(TAG,String.format("wid=%d, hei=%d",paramsGuide.width,paramsGuide.height));

        paramsDraw.width = panelWidth;
        paramsDraw.height = panelHeight;



        Resources resources = getResources();
        Configuration config = resources.getConfiguration();
        //DisplayMetrics dm = resources.getDisplayMetrics();
        if(mGlobalVar.getCurLanguageIndex()==0)
        {
            config.locale = Locale.getDefault();
            //上面是对应三种情况下设置三种语言设置
            resources.updateConfiguration(config, dm);//刷新语言设置信息，这步不能少。
        }
        else if(mGlobalVar.getCurLanguageIndex()==1)    // En
        {
            config.locale = Locale.ENGLISH;
            //上面是对应三种情况下设置三种语言设置
            resources.updateConfiguration(config, dm);//刷新语言设置信息，这步不能少。
        }
        else if(mGlobalVar.getCurLanguageIndex()==2)    // En
        {
            config.locale = Locale.CHINA;
            //上面是对应三种情况下设置三种语言设置
            resources.updateConfiguration(config, dm);//刷新语言设置信息，这步不能少。
        }

        gameView = new MyDrawView(this);

        gameView.screenWidth_Pass = mGlobalVar.getScreenHeight();
        gameView.screenHeight_Pass = mGlobalVar.getScreenWidth();
        LayoutInflater inflater = LayoutInflater.from(getApplication());

        //获取浮动窗口视图所在布局.
        guideLayout = (AbsoluteLayout) inflater.inflate(R.layout.user_guide_layout, null);
        layout_user_guide_tip = (RelativeLayout) guideLayout.findViewById(R.id.layout_user_guide_tip);
        layout_advance_setting = (AbsoluteLayout) inflater.inflate(R.layout.advance_setting_layout, null);
        layout_advance_setting_step3 = (LinearLayout) layout_advance_setting.findViewById(R.id.layout_advance_setting_step3);
        layout_advance_setting_step3.setVisibility(View.GONE);


        gameView.bHasNotchScreen = mGlobalVar.getIsNotchFlag();


        Log.d(TAG,String.format("left=%d,top= %d, wid=%d, hei=%d",paramsDraw.x,paramsDraw.y,paramsDraw.width,paramsDraw.height));
        windowManager.addView(gameView, paramsDraw);
        windowManager.addView(guideLayout, paramsGuide);


        iv_user_guide_btn = (ImageView) guideLayout.findViewById(R.id.iv_user_guide_btn);
        iv_user_guide_ring = (ImageView) guideLayout.findViewById(R.id.iv_user_guide_ring);
        tv_tip_user_guide = (TextView) guideLayout.findViewById(R.id.tv_tip_user_guide);
        tv_tip_user_guide_detail = (TextView) guideLayout.findViewById(R.id.tv_tip_user_guide_detail);
        btn_skip_user_guide = (Button) guideLayout.findViewById(R.id.btn_skip_user_guide);
        btn_user_guide_next = (Button) guideLayout.findViewById(R.id.btn_user_guide_next);
       // btn_skip_user_guide.setX(panelWidth/4+dip2px(this,265));
       // btn_skip_user_guide.setY(0);

        ViewGroup.LayoutParams lp = iv_user_guide_btn.getLayoutParams();
        mImgBtnSize = lp.height/2 ;//dip2px(this,20);

        ViewGroup.LayoutParams ringlp = iv_user_guide_ring.getLayoutParams();
        mImgRingSize = ringlp.height/2 ;//dip2px(this,20);

        //ViewGroup.LayoutParams lp = iv_tip_user_guide.getLayoutParams();
        iv_user_guide_btn.setVisibility(View.GONE);
        iv_user_guide_btn.setX(dip2px(this, 100));
        iv_user_guide_btn.setY(dip2px(this, 100));
        iv_user_guide_btn.setVisibility(View.VISIBLE);

        layout_user_guide_tip.setX((int)(panelWidth/2)-dip2px(this,150));
        layout_user_guide_tip.setY(dip2px(this,80));

        if(Transfer_Con.ENABLE_S2_QUICK_MENU||Transfer_Con.ENABLE_GN_LOGO_QUICK_MENU)
        {
            if(mGlobalVar.getIsSupportMenuKey())
            {
                int x = (int)(layout_user_guide_tip.getX()+30);
                int y = (int)(layout_user_guide_tip.getY()+dip2px(this,140)-30);
                x = (x + 1) * 0x7FFF / panelWidth;
                y = (y+1)*0x7FFF / panelHeight;
                mGlobalVar.tranSet_Value(Transfer_Con.HID_CMD_SET_MENU_POS, 1, (x << 16) | y);
            }
        }

        if(mGlobalVar.getIsEnterAdvGuide()) {
            tv_tip_user_guide.setText(getResources().getString(R.string.string_pop_user_guide_adv_step_1));
            tv_tip_user_guide_detail.setText(getResources().getString(R.string.string_pop_user_guide_adv_detail_1));
            gameView.bIsAdvSettingGuide=true;
        }
        else if(mGlobalVar.getIsEnterGameGuide())
        {
            tv_tip_user_guide.setText("1、按键特殊功能开关");
            tv_tip_user_guide_detail.setText("在游戏当中可以查看或者开关按键的高级设置");
            iv_user_guide_btn.setVisibility(View.GONE);
            gameView.bIsEnterGameGuide=true;
            gameView.mStep = 0;
        }
        else if(mGlobalVar.getIsFloatMenuGuide())
        {
            tv_tip_user_guide.setText(getResources().getString(R.string.string_pop_user_guide_float_step_1));
            tv_tip_user_guide_detail.setText(getResources().getString(R.string.string_pop_user_guide_float_detail_1));
            iv_user_guide_btn.setVisibility(View.GONE);
            gameView.bIsFloatMenuGuide = true;
            gameView.mStep = 0;
        }
        guideLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.d(TAG,String.format("onTouch x=%f,y=%f",event.getRawX(),event.getRawY()));
                if(mGlobalVar.getIsEnterAdvGuide()) {
                    if (gameView.mStep == 1) {
                        if ((Math.abs((int) event.getRawX() - (panelWidth / 4 + dip2px(UserGuideService.this, 200))) <= 50) && ((int) event.getRawY() < dip2px(UserGuideService.this, 30))) {
                            btn_user_guide_next.performClick();
                        }

                    } else if (gameView.mStep == 2) {
                        if ((Math.abs((int) event.getRawX() - (panelWidth / 4 + dip2px(UserGuideService.this, 145))) <= 50) && ((int) event.getRawY() < dip2px(UserGuideService.this, 30))) {
                            btn_user_guide_next.performClick();

                        }

                    } else if (gameView.mStep == 3) {
                        if ((Math.abs((int) event.getRawX() - (panelWidth / 4 + dip2px(UserGuideService.this, 95))) <= 50) && ((int) event.getRawY() < dip2px(UserGuideService.this, 30))) {
                            btn_user_guide_next.performClick();
                        }

                    } else if (gameView.mStep == 4) {
                        if ((Math.abs((int) event.getRawX() - (panelWidth / 4 + dip2px(UserGuideService.this, 55))) <= 50) && ((int) event.getRawY() < dip2px(UserGuideService.this, 30))) {
                            btn_user_guide_next.performClick();
                        }

                    } else if (gameView.mStep == 6) {
                        if (((int) event.getRawX() >= (100)) && ((int) event.getRawX() <= (100 + dip2px(UserGuideService.this, 170)))
                                && ((int) event.getRawY() >= (dip2px(UserGuideService.this, 35)))) {
                            btn_user_guide_next.performClick();
                        }
                    }
                }
                else if(mGlobalVar.getIsEnterGameGuide())
                {

                }
                return false;
            }
        });

        btn_skip_user_guide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Log.d(TAG,"skip guide");
                if(mGlobalVar.getIsEnterGameGuide())
                {
                    mGlobalVar.setIsEnterGameGuide(false);
                    mGlobalVar.setIsEnterGameNeedGuide(false);
                    sendContentBroadcast("Game");
                    Log.d(TAG,"skip guide Game");
                }
                if(mGlobalVar.getIsEnterAdvGuide())
                {
                    if(Transfer_Con.ENABLE_S2_QUICK_MENU||Transfer_Con.ENABLE_GN_LOGO_QUICK_MENU) {
                        if (mGlobalVar.getIsSupportMenuKey())
                        {
                            int x = mGlobalVar.menuOnPos_x;
                            int y = mGlobalVar.menuOnPos_y;
                            mGlobalVar.tranSet_Value(Transfer_Con.HID_CMD_SET_MENU_POS, 1, (x << 16) | y);
                        }
                    }

                    mGlobalVar.setIsEnterAdvGuide(false);
                    mGlobalVar.setIsAdvSetNeedGuide(false);
                    sendContentBroadcast("Setting");
                    Log.d(TAG,"skip guide Setting");
                }
                if(mGlobalVar.getIsFloatMenuGuide())
                {
                    if(Transfer_Con.ENABLE_S2_QUICK_MENU||Transfer_Con.ENABLE_GN_LOGO_QUICK_MENU) {
                        if (mGlobalVar.getIsSupportMenuKey())
                        {
                            int x = mGlobalVar.menuOnPos_x;
                            int y = mGlobalVar.menuOnPos_y;
                            mGlobalVar.tranSet_Value(Transfer_Con.HID_CMD_SET_MENU_POS, 1, (x << 16) | y);
                        }
                    }
                    mGlobalVar.setIsFloatMenuGuide(false);
                    sendContentBroadcast("Float");
                    Log.d(TAG,"skip Float guide");
                }
                gameView.mStep = 0;
                stopSelf();
            }
        });

        btn_user_guide_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                gameView.mStep++;
                gameView.externOnDraw();
                if(mGlobalVar.getIsEnterAdvGuide()) {
                    switch (gameView.mStep) {
                        case 0:
                            break;
                        case 1:
                            tv_tip_user_guide.setText(getResources().getString(R.string.string_pop_user_guide_adv_step_2));
                            tv_tip_user_guide_detail.setText(getResources().getString(R.string.string_pop_user_guide_adv_detail_2));
                            break;
                        case 2:
                            tv_tip_user_guide.setText(getResources().getString(R.string.string_pop_user_guide_adv_step_3));
                            tv_tip_user_guide_detail.setText(getResources().getString(R.string.string_pop_user_guide_adv_detail_3));
                            break;
                        case 3:
                            tv_tip_user_guide.setText(getResources().getString(R.string.string_pop_user_guide_adv_step_4));
                            tv_tip_user_guide_detail.setText(getResources().getString(R.string.string_pop_user_guide_adv_detail_4));
                            break;
                        case 4:
                            tv_tip_user_guide.setText(getResources().getString(R.string.string_pop_user_guide_adv_step_5));
                            tv_tip_user_guide_detail.setText(getResources().getString(R.string.string_pop_user_guide_adv_detail_5));
                            break;

                        case 5:
                            tv_tip_user_guide.setText(getResources().getString(R.string.string_pop_user_guide_adv_step_6));
                            tv_tip_user_guide_detail.setText(getResources().getString(R.string.string_pop_user_guide_adv_detail_6));
                            iv_user_guide_btn.setX(dip2px(UserGuideService.this,300)-mImgBtnSize);
                            iv_user_guide_btn.setY(dip2px(UserGuideService.this,300)-mImgBtnSize);


                            iv_user_guide_ring.setX(dip2px(UserGuideService.this,300)-(mImgRingSize));
                            iv_user_guide_ring.setY(dip2px(UserGuideService.this,300)-(mImgRingSize));
                            iv_user_guide_ring.setVisibility(View.VISIBLE);
                            //btn_user_guide_next.setVisibility(View.GONE);
                            break;

                        case 6:
                            iv_user_guide_btn.setVisibility(View.GONE);
                            iv_user_guide_ring.setVisibility(View.GONE);
                            windowManager.removeView(gameView);
                            windowManager.removeView(guideLayout);
                            windowManager.addView(layout_advance_setting, paramsAdvance);
                            windowManager.addView(gameView, paramsDraw);
                            windowManager.addView(guideLayout, paramsGuide);
                            btn_skip_user_guide.setVisibility(View.GONE);
                            tv_tip_user_guide.setText(getResources().getString(R.string.string_pop_user_guide_adv_step_7));
                            tv_tip_user_guide_detail.setText(getResources().getString(R.string.string_pop_user_guide_adv_detail_7));
                            btn_user_guide_next.setText(getResources().getString(R.string.string_pop_user_guide_adv_finish));
                            break;
                        case 7:
                            gameView.mStep = 0;
                            mGlobalVar.setIsEnterAdvGuide(false);
                            mGlobalVar.setIsAdvSetNeedGuide(false);
                            windowManager.removeView(layout_advance_setting);
                            sendContentBroadcast("Setting");
                            stopSelf();
                            break;

                        default:
                            break;
                    }
                }
                else if(mGlobalVar.getIsEnterGameGuide())
                {
                    switch (gameView.mStep)
                    {
                        case 1:
                            tv_tip_user_guide.setText("2、按键");
                            tv_tip_user_guide_detail.setText("第一列为拥有特殊功能的按键。");
                            break;
                        case 2:
                            tv_tip_user_guide.setText("3、按键特殊功能名称");
                            tv_tip_user_guide_detail.setText("第二列为拥有特殊功能的名称，具体描述请到按键设置里面的高级设置查看。");
                            break;
                        case 3:
                            tv_tip_user_guide.setText("3、按键特殊快捷开关");
                            tv_tip_user_guide_detail.setText("第三列为拥有特殊功能的快捷开关，开启为特殊功能，关闭则变为普通的点击动作。");
                            break;
                        case 4:
                            tv_tip_user_guide.setText("4、悬浮图标");
                            tv_tip_user_guide_detail.setText("快捷菜单在一段时间内没有操作则会隐藏起来，点击悬浮图标将会再次出现；也可以点击悬浮图标快速隐藏/显示快捷菜单。");

                            break;
                        case 5:
                            tv_tip_user_guide.setText("5、返回");
                            tv_tip_user_guide_detail.setText("点击这个图标可以返回主界面。");
                            btn_user_guide_next.setText("结束");
                            break;
                        case 6:
                            gameView.mStep = 0;
                            mGlobalVar.setIsEnterGameNeedGuide(false);
                            sendContentBroadcast("Game");
                            stopSelf();
                            break;
                            default:
                                break;
                    }
                }
                else if(mGlobalVar.getIsFloatMenuGuide())
                {
                    switch (gameView.mStep)
                    {
                        case 1:
                            tv_tip_user_guide.setText(getResources().getString(R.string.string_pop_user_guide_float_step_2));
                            tv_tip_user_guide_detail.setText(getResources().getString(R.string.string_pop_user_guide_float_detail_2));
                            break;
                        case 2:
                            tv_tip_user_guide.setText(getResources().getString(R.string.string_pop_user_guide_float_step_3));
                            tv_tip_user_guide_detail.setText(getResources().getString(R.string.string_pop_user_guide_float_detail_3));
                            break;
                        case 3:
                            tv_tip_user_guide.setText(getResources().getString(R.string.string_pop_user_guide_float_step_4));
                            tv_tip_user_guide_detail.setText(getResources().getString(R.string.string_pop_user_guide_float_detail_4));
                            break;
                        case 4:
                            tv_tip_user_guide.setText(getResources().getString(R.string.string_pop_user_guide_float_step_5));
                            tv_tip_user_guide_detail.setText(getResources().getString(R.string.string_pop_user_guide_float_detail_5));
                            break;
                        case 5:
                            tv_tip_user_guide.setText(getResources().getString(R.string.string_pop_user_guide_float_step_6));
                            tv_tip_user_guide_detail.setText(getResources().getString(R.string.string_pop_user_guide_float_detail_6));
                            break;
                        case 6:
                            tv_tip_user_guide.setText(getResources().getString(R.string.string_pop_user_guide_float_step_7));
                            tv_tip_user_guide_detail.setText(getResources().getString(R.string.string_pop_user_guide_float_detail_7));
                            break;
                        case 7:
                            tv_tip_user_guide.setText(getResources().getString(R.string.string_pop_user_guide_float_step_8));
                            tv_tip_user_guide_detail.setText(getResources().getString(R.string.string_pop_user_guide_float_detail_8));
                            break;
                        case 8:
                            tv_tip_user_guide.setText(getResources().getString(R.string.string_pop_user_guide_float_step_9));
                            tv_tip_user_guide_detail.setText(getResources().getString(R.string.string_pop_user_guide_float_detail_9));
                            break;
                        case 9:
                            gameView.mStep = 0;
                            mGlobalVar.setIsFloatMenuGuide(false);
                            sendContentBroadcast("Float");
                            stopSelf();
                            break;
                        default:
                            break;
                    }
                }


            }
        });

        iv_user_guide_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG,"onClick");
                if(gameView.mStep == 5)
                {
                    Log.d(TAG,"go step6");
                    iv_user_guide_ring.setVisibility(View.GONE);
                    btn_user_guide_next.setVisibility(View.VISIBLE);
                    btn_user_guide_next.performClick();
                    //
                }
            }
        });

        iv_user_guide_btn.setOnTouchListener(new View.OnTouchListener() {
            int prevX=0,prevY=0;
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                int pos_x,pos_y;

                //Log.d(TAG,event.toString());
                gameView.onTouchEvent(event);
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        Log.i(TAG,"ACTION_DOWN");
                        prevX = (int)event.getRawX();
                        prevY = (int)event.getRawY();
                        if(!bIsBigIcon) {
                            iv_user_guide_ring.setX(v.getX()-(mImgRingSize-mImgBtnSize));
                            iv_user_guide_ring.setY(v.getY()-(mImgRingSize-mImgBtnSize));
                            iv_user_guide_ring.setVisibility(View.VISIBLE);
                            bIsBigIcon = true;
                        }
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if((Math.abs(prevX-(int)event.getRawX())>10)||(Math.abs(prevY-(int)event.getRawY())>10)) {
                            pos_x = (int) event.getRawX() - mImgBtnSize;
                            pos_y = (int) event.getRawY() - mImgBtnSize;
                            //Log.i(TAG, String.format("ACTION_MOVE x=%d, y=%d", pos_x, pos_y));
                            v.setX(pos_x);
                            v.setY(pos_y);
                            iv_user_guide_ring.setX(pos_x-(mImgRingSize-mImgBtnSize));
                            iv_user_guide_ring.setY(pos_y-(mImgRingSize-mImgBtnSize));
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        Log.i(TAG,"ACTION_UP");
                        iv_user_guide_ring.setX(v.getX()-(mImgRingSize-mImgBtnSize));
                        iv_user_guide_ring.setY(v.getY()-(mImgRingSize-mImgBtnSize));
                        break;
                }
                return false;
            }
        });

        myBroadcastReceive = new MyBroadcastReceive();
//注册广播接收,注意：要监听这个系统广播就必须用这种方式来注册，不能再xml中注册，否则不能生效
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.intent.action.CONFIGURATION_CHANGED");
        filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
        filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
        registerReceiver(myBroadcastReceive,filter);
    }
    @Override
    public void onDestroy()
    {

        unregisterReceiver(myBroadcastReceive);
        if(mGlobalVar.getIsEnterAdvGuide())
        {
            if(gameView.mStep==6)
            {
                gameView.mStep = 0;
                windowManager.removeView(layout_advance_setting);
            }
        }
        windowManager.removeView(gameView);
        windowManager.removeView(guideLayout);
        super.onDestroy();
    }

    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    class MyBroadcastReceive extends BroadcastReceiver
    {
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            Configuration mConfiguration = context.getResources().getConfiguration(); //获取设置的配置信息
            int ori = mConfiguration.orientation; //获取屏幕方向
            if (ori == Configuration.ORIENTATION_LANDSCAPE) {
                //横屏
                Log.d(TAG,"横屏 "+String.valueOf(ori));
            } else if (ori == Configuration.ORIENTATION_PORTRAIT) {
                //竖屏
                Log.d(TAG,"竖屏 "+String.valueOf(ori));
                stopSelf();
            }
            String action=intent.getAction();
            if(UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action))
            {
                mGlobalVar.setbIsUsbConnect(false);
                mGlobalVar.setbIsTransferMode(false);
                mGlobalVar.myUsbDevice = null;
                mGlobalVar.myInterface = null;
                mGlobalVar.epOut = null;
                mGlobalVar.epIn = null;
                stopSelf();
            }
        }
    }

    protected void sendContentBroadcast(String name)
    {
        Intent intent = new Intent();
        intent.setAction("com.opd.soeghtool.content");
        intent.putExtra("Guide",name);
        sendBroadcast(intent);
    }

}
